package edu.neusoft.android.yuejian14.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.javabean.Course;

import static edu.neusoft.android.yuejian14.Const_Pic.BASE_URL;

public class CourseListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<Course> mDataList;
    private LayoutInflater mLayoutInflater;

    public CourseListAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public static ImageView pic;
        TextView course_name, course_intro, course_price;


        public ViewHolder(View itemView) {
            super(itemView);
            course_name = (TextView) itemView.findViewById(R.id.course_name);
            pic = (ImageView) itemView.findViewById(R.id.course_pic);
            course_intro = (TextView) itemView.findViewById(R.id.course_intro);
            course_price = (TextView) itemView.findViewById(R.id.course_price);

        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.item_course, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Course entity = (Course) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.course_name.setText(entity.getC_name());
        viewHolder.course_price.setText("￥" + entity.getC_price());
        viewHolder.course_intro.setText(entity.getC_intro());

        String course_pic = BASE_URL + entity.getC_pic();
        if (!course_pic.equals("")) {
            Picasso.with(mContext).load(course_pic).error(R.drawable.no_pic).into(ViewHolder.pic);
        }
//        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                Intent intent = new Intent(mContext, CourseListActivity.class);
//                intent.putExtra("course_id", entity.getCid());
//                mContext.startActivity(intent);
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void setData(List<Course> list) {
        mDataList = list;
        notifyDataSetChanged();
    }

    public Course getItem(int count) {
        return mDataList != null ? mDataList.get(count) : null;
    }
}
